import easygui as g
import random as r
import os
import chardet

def randomGame():
    g.msgbox(msg='hi，欢迎进入第一个界面小游戏')
    secret = r.randint(1,100)

    msg = '猜猜看吧，数字'
    title = '数字游戏'
    guess = g.integerbox(msg=msg,title=title)

    while True:
        if guess == secret:
            g.msgbox('卧槽，这也猜到了')
            g.msgbox('然而没有奖励')
            break
        else:
            if guess > secret:
                g.msgbox('我的哥，大了大了')
            else:
                g.msgbox('小了小了')
        guess = g.integerbox(msg=msg,title=title)
# randomGame()

def register():
    msg = '请填写以下联系方式'
    title = '账号中心'
    fieldNames = ['*用户名','*真实姓名','固定电话','*手机号码','qq','*E-mail']
    fieldValues = []
    fieldValues = g.multenterbox(msg,title,fieldNames)

    while True:
        if fieldValues == None:
            break

        errmsg = ''
        for i in range(len(fieldNames)):
            option = fieldNames[i].strip()
            if fieldValues[i].strip() == '' and option[0] =='*':
                errmsg += ('【%s】为必填项\n' %fieldNames[i])
        if errmsg == '':
            break
        fieldValues = g.multenterbox(errmsg,title,fieldNames,fieldValues)
    print('用户资料如下：%s' % str(fieldValues))
# register()

def openfile():
    file_path = g.fileopenbox(default='*.txt')
    try:
        with open(file_path) as f:
            title = os.path.basename(file_path)

            msg = '文件【%s】的内容如下:' %title
            text = f.read()
            text_after = g.textbox(msg,title,text)

            if text != text_after:
                choice = g.buttonbox('检测到文件内容发生改变，请选择以下操作',
                                     '警告',('覆盖保存','放弃保存','另存为...'))
                if choice =='覆盖保存':
                    with open(file_path,'w') as oldfile:
                        oldfile.write(text_after[0:-1])

                elif choice =='放弃保存':
                    pass
                elif choice == '另存为...':
                    other_path = g.filesavebox(default='.txt')

                    if os.path.splitext(other_path)[1] != '.txt':
                        other_path += '.txt'

                    with open(other_path,'w') as new_file:
                        new_file.write(text_after[0:-1])

    except UnicodeDecodeError as e:
        g.msgbox(e)
# openfile()
# g.filesavebox(msg='aa',title='bb',default='fff',filetypes='*.jpg')

